package cn.itcast.web.controller.cargo;


import cn.hutool.core.util.StrUtil;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping(value = "/cargo/factory",name = "工厂模块")
public class FactoryController extends BaseController {
    @Reference
    private FactoryService factoryService;

    @RequestMapping(value = "/list",name = "厂家信息查询")
    public String list(@RequestParam(defaultValue = "1")Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        //
        FactoryExample example = new FactoryExample();
        PageInfo page =factoryService.findByPage(pageNum,pageSize,example);
        request.setAttribute("page",page);
        return "cargo/factory/factory-list";
    }
    @RequestMapping(value = "/toAdd",name = "新建")
    public String toAdd(){
        return "cargo/factory/factory-add";
    }
    @RequestMapping(value = "/edit",name = "新增或修改")
    public String edit(Factory factory){
        if (StrUtil.hasEmpty(factory.getId())){
            factory.setCreateTime(new Date());
            factory.setCreateBy(getLoginUser().getId());
            factory.setCreateDept(getLoginUser().getDeptId());
            factoryService.save(factory);
        }else{
            factory.setUpdateBy(getLoginUser().getId());
            factory.setUpdateTime(new Date());
            factoryService.update(factory);
        }

        return "redirect:/cargo/factory/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "修改")
    public String toUpdate(String id){
        Factory factory = factoryService.findById(id);
        request.setAttribute("factory",factory);
        return "cargo/factory/factory-update";
    }
    @RequestMapping(value = "/delete",name = "删除")
    public String delete(String id){
        factoryService.delete(id);
        return "redirect:/cargo/factory/list.do";
    }
}
